package com.example.animationmodule

import android.app.Activity
import android.os.*
import android.view.animation.Animation
import android.view.animation.LinearInterpolator
import android.view.animation.RotateAnimation
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import kotlin.concurrent.thread

class SaomiaoActivity : AppCompatActivity() {

	 lateinit var iv_main_scan: ImageView
	 lateinit var tv_main_scan: TextView
	 lateinit var pb_main_scan: ProgressBar

	private val myHandler = object:Handler(Looper.getMainLooper()){
		override fun handleMessage(msg: Message) {
			when(msg.what){
				1->{
					pb_main_scan.progress+=1
				}
				2->
				{
					tv_main_scan.text="扫描结束"
					iv_main_scan.clearAnimation()
				}

			}
		}
	}
	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)
		setContentView(R.layout.layout_saomiao)

		this.iv_main_scan = findViewById(R.id.iv_main_scan)
		this.tv_main_scan = findViewById(R.id.tv_main_scan)
		this.pb_main_scan = findViewById(R.id.pb_main_scan)

		//1、显示扫描动画
		showScanAnimation()

		//2、扫描，并显示扫描进度
//		Thread.sleep(10000)
		scan()
	}

	private fun scan() {
//		TODO("Not yet implemented")
		this.tv_main_scan.text="手机杀毒引擎正在扫描中……"
		thread {
			val appCount=60
			this.pb_main_scan.max=appCount
			for (i in 0 until 60 step 1)
			{
				SystemClock.sleep(100)
				this.myHandler.sendEmptyMessage(1)
			}
			this.myHandler.sendEmptyMessage(2)
		}


	}

	private fun showScanAnimation() {
//		TODO("Not yet implemented")
		val rotateAnimation = RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f,
				Animation.RELATIVE_TO_SELF, 0.5f).apply {
			duration = 1000
			repeatCount = Animation.INFINITE
			interpolator=LinearInterpolator()
		}
		this.iv_main_scan.startAnimation(rotateAnimation)


	}
}
